package com.symaster.gameengine.test;

import com.symaster.common.geom.util.PointUtil;
import com.symaster.gameengine.core.Renderer;
import com.symaster.gameengine.e2d.transform.Transform;
import org.lwjgl.opengl.GL11;

/**
 * @author yinmiao
 * @version 2023/11/16
 */
public class AngleViewRenderer extends Renderer {
    @Override
    public int getHeight() {
        return 60;
    }

    @Override
    public int getWidth() {
        return 60;
    }

    /**
     * 子组件实现绘制功能
     */
    @Override
    protected void draw() {

        GL11.glColor4f(1 , 0, 0, 1);

        // 绘制一个圆
        drawCircle(0.0f, 0.0f, 0.9f, 100);

        // 重置颜色设置
        GL11.glColor4f(0, 0, 1 , 1);

        drawAngle();

        GL11.glColor4f(1, 1, 1, 1);
    }

    public void drawCircle(float cx, float cy, float r, int numSegments) {
        GL11.glBegin(GL11.GL_TRIANGLE_FAN);
        // GL11.glClearColor(1, 0, 0, 1);

        // 中心点
        GL11.glVertex2f(cx, cy);

        // 绘制圆上的各个顶点
        for (int i = 0; i <= numSegments; i++) {
            double theta = 2.0 * Math.PI * i / numSegments; // 当前角度
            float x = (float) (r * Math.cos(theta)); // 计算 x 坐标
            float y = (float) (r * Math.sin(theta)); // 计算 y 坐标

            GL11.glVertex2f(cx + x, cy + y); // 绘制顶点
        }

        GL11.glEnd();
    }

    public void drawAngle() {
        GL11.glBegin(GL11.GL_LINES);

        double[] doubles = PointUtil.rotateTransform(0, 1, 0, 0, getAngle());

        GL11.glVertex2f((float) doubles[0], (float) doubles[1]);
        GL11.glVertex2f(0, 0);


        GL11.glEnd();
    }

    private float getAngle() {
        Transform component = getComponent(Transform.class);
        if (component == null) {
            return 0;
        }

        return component.getAngle();
    }
}
